import { isJson } from '@/utils/validate.js'

export function useQuestionnaire() {
  function previewData(wjxData) {
    // isString
    const data = isJson(wjxData)
    if (data && data.length > 0) {
      const qList = data.reduce((pre, cur) => {
        const { items, actives } = cur.value.reduce(
          (subPre, subCur) => {
            const { actives, items } = subPre
            const { key, active } = subCur
            items.push({ label: key, value: key })
            if (active) actives.push(key)
            return { actives, items }
          },
          { actives: [], items: [] }
        )
        pre.push({ title: cur.key, items, actives: actives.join(',') })
        return pre
      }, [])
      return qList
    } else {
      return null
    }
  }
  return {
    previewData,
  }
}
